// fetch 有兼容问题，此部分仅模拟接口请求的封装
const request = (url: string, params?: any): Promise<any> => {
  return new Promise((resolve, reject) => {
    let content = {}
    if (params) {
      content = {
        method: 'post',
        body: JSON.stringify(params)
      }
    }
    fetch(url, content)
      .then((resp) => resp.json())
      .then((res) => {
        if (res?.head && res?.head.code !== 100000) {
          console.log(res?.msg)
          return reject(new Error(res?.msg || 'Error'))
        } else {
          return resolve(res)
        }
      })
  })
}

const responseBody = async (url: string, params?: any): Promise<any> => {
  try {
    const res = await request(url, params)
    return res.body
  } catch (error) {
    console.log(error)
  }
}

export {
  request,
  responseBody
}
